layui.use(['form', 'layer', 'jquery', 'laydate'],
    function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer
            , laydate = layui.laydate;
        //日期
        laydate.render({
            elem: '#birthday',
            trigger: 'click'
        });
        //自定义验证规则
        form.verify({
            nikename: function(value) {
                if (value.length < 5) {
                    return '昵称至少得5个字符啊';
                }
            },
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            repass: function(value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });
        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function () {
            var data = form.val('example');
            alert(JSON.stringify(data));
            // console.log(data)
        });
        //监听提交
        form.on('submit(add)',
            function (data) {
                data = data.field;
                let arr_box = [];
                $('input[type=checkbox][name=hobby]:checked').each(function () {
                    arr_box.push($(this).attr("title"));
                });
                //数组
                // console.log(arr_box);
                //js 将数组转换位字符串
                // console.log(data);
                data.hobby = arr_box.toString();
                // let aa=form.val('userAdd');
                //  console.log(JSON.stringify(aa));
                //发异步，把数据提交给php
                $.ajax({
                    url: '/back/user/add',
                    data: data,
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        if (data.msg == 'success') {
                            layer.alert("增加成功", {
                                    icon: 6
                                },
                                function () {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        } else {
                            layer.msg("新增用户失败");
                        }
                    }
                });
                return false;
            });

    });